package com.lemon.smartsafetynew.ui.fragment.incident;

import android.os.Bundle;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.incident.IncidentVo;
import com.lemon.smartsafetynew.net.services.IncidentServices;
import com.lemon.smartsafetynew.ui.activity.incident.IncidentSafePatrolProcessActivity;
import com.lemon.smartsafetynew.ui.adapter.incident.IncidentListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/9/6.
 */

public class IncidentListFragment extends BasePullToRefreshFragment<IncidentVo, List<IncidentVo>>
        implements RecyclerTClickListener<IncidentVo> {

    private String status = "";
    private User user;

    public static IncidentListFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString(KeyConstants.KEY_INCIDENT_LIST_TYPE, type);

        IncidentListFragment fragment = new IncidentListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            status = bundle.getString(KeyConstants.KEY_INCIDENT_LIST_TYPE);
        }

    }

    @Override
    public void onPullRefresh() {

        user = AccountUtil.getUser();
        if (user == null) {
            return;
        }

        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", status);
        params.put("company_id", user.getCompanyId());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        //Log.e("yxy", "onPullRefresh params = " + params );
        IncidentServices
                .init()
                .getIncidentList(RefreshType.REFRESH, params, status);

    }

    @Override
    public void onLoadMore() {

        user = AccountUtil.getUser();
        if (user == null) {
            return;
        }

        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", status);
        params.put("company_id", user.getCompanyId());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        //Log.e("yxy", "onLoadMore params = " + params );
        IncidentServices
                .init()
                .getIncidentList(RefreshType.LOAD_MORE, params, status);
    }

    @Override
    public void onItemClick(IncidentVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_INCIDENT_OBJ, data);
        UIHelper.showActivity(getmContext(), bundle, IncidentSafePatrolProcessActivity.class);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<IncidentVo> getLemonAdapter() {
        return new IncidentListAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_INCIDENT_LIST + status;
    }

    /**
     * 下拉刷新
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ViewEvent event) {
        if (IncidentSafePatrolProcessActivity.class.getName().equals(event.getTag())) {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
            mPullToRefreshRecyclerView.beginRefreshing();
        }
    }

}
